package com.p2l.letterblocks;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.Button;

public class DialogSettings extends Dialog {
	private SharedPreferences preferences;
	private SeekBar music, effects, voice;
	private RadioButton left, right;
	private Button save;

	public DialogSettings(Context context) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		setContentView(R.layout.settings);
		
		preferences = context.getSharedPreferences("LetterBlocks", Activity.MODE_PRIVATE);
		
		float m = preferences.getFloat("music", 1.0f);
		float e = preferences.getFloat("effects", 1.0f);
		float v = preferences.getFloat("voice", 1.0f);
		int h = preferences.getInt("hand", 1);
		
		music = (SeekBar) findViewById(R.id.sbrMusic);
		effects = (SeekBar) findViewById(R.id.sbrEffects);
		voice = (SeekBar) findViewById(R.id.sbrVoice);
		left = (RadioButton) findViewById(R.id.rdoLeft);
		right = (RadioButton) findViewById(R.id.rdoRight);
		save = (Button) findViewById(R.id.btnSettingsSave);
		
		music.setProgress((int)(m * 10));
		effects.setProgress((int)(e * 10));
		voice.setProgress((int)(v * 10));
		if (h == 1)
			right.setChecked(true);
		else
			left.setChecked(true);
		
		save.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Editor editor = preferences.edit();
				editor.putFloat("music", music.getProgress() / 10.0f);
				editor.putFloat("effects", effects.getProgress() / 10.0f);
				editor.putFloat("voice", voice.getProgress() / 10.0f);
				editor.putInt("hand", (right.isChecked() ? 1 : 0));
				editor.commit();
				cancel();
			}
		});
	}
}